
using Azure.Core;
using B.S.Data.MES.API.Applications.AccountApplication.Command;
using B.S.Data.MES.API.Applications.approve.Command;
using B.S.Data.MES.API.Applications.BaseData.Command.BOMCommand;
using B.S.Data.MES.API.Applications.BaseData.Command.ChanXIanCommand;
using B.S.Data.MES.API.Applications.BaseData.Command.CheJianCommand;
using B.S.Data.MES.API.Applications.BaseData.Command.CustomerCommand;
using B.S.Data.MES.API.Applications.BaseData.Command.CustomerCommand;
using B.S.Data.MES.API.Applications.BaseData.Command.MaterialGroupCommand;
using B.S.Data.MES.API.Applications.BaseData.Command.ProductCommand;
using B.S.Data.MES.API.Applications.BaseData.Command.ProductMaterialCommand;
using B.S.Data.MES.API.Applications.BaseData.Command.SupplierCommand;
using B.S.Data.MES.API.Applications.BaseData.CommandHandlers.BOMCommandHandlers;
using B.S.Data.MES.API.Applications.BaseData.CommandHandlers.ChejianCommandHandlers;
using B.S.Data.MES.API.Applications.CangKu.Command;
using B.S.Data.MES.API.Applications.CreateTimeApplication;
using B.S.Data.MES.API.Applications.Management.SteamApplication.Command.FactoryCommand;
using B.S.Data.MES.API.Applications.Management.SteamApplication.Command.OrcanCommand;
using B.S.Data.MES.API.Applications.Management.SteamApplication.Command.RoleCommand;
using B.S.Data.MES.API.Applications.QualityManagement.ExamineDesignCommand.Commands;
using B.S.Data.MES.API.Applications.QualityManagement.ExamineDesignCommand.Commands;
using B.S.Data.MES.API.Applications.QualityManagement.ExamineMaterialsCommand.Commands;
using B.S.Data.MES.API.Applications.QualityManagement.ExamineProjectCommand.Commands;
using B.S.Data.MES.API.Applications.QualityManagement.ExamineTaskCommand.Commands;
using B.S.Data.MES.API.Applications.QualityManagement.ExamineTypeModel.Command;
using B.S.Data.MES.API.Applications.ShenPi.Command;
using B.S.Data.MES.API.Dto.LoginDTO;
using B.S.Domain.CangKu;
using B.S.Domain.Date;
using B.S.Domain.审计字段;
using B.S.Infratrctrue;
using B.S.Requst;
using B.S.Requst;
using ERP.Requst;
using MediatR;
using MediatR;
using MediatR;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Components.Forms;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore.Migrations.Operations.Builders;
using MiniExcelLibs;
using NPOI.HSSF.UserModel;
using NPOI.SS.UserModel;
using NPOI.XSSF.UserModel;
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.IO;
using System.Linq;
using System.Net;
using System.Security.Claims;
using System.Threading.Tasks;
using System.Transactions;
using static Microsoft.EntityFrameworkCore.DbLoggerCategory;

namespace B.S.Data.MES.API.Controllers
{
    /// <summary>
    /// 写的控制器
    /// </summary>
    [Route("api/[controller]/[action]")]
    [ApiController]
    [Authorize]
    public class WriteController : ControllerBase
    {
        private readonly IBaseRepository<CangKuinfo> cangRepsoitory;
        private readonly IBaseRepository<CangTypeinfo> _cangTypeRepository; // 新增仓库类型仓储

        /// <summary>
        /// Mes写入
        /// </summary>
        /// <param name="mediator">中介者</param>
        /// <param name="logger">日志</param>
        /// <param name="cangRepsoitory">仓库仓储</param>
        /// <param name="cangTypeRepository">仓库类型仓储</param>
        public WriteController(
            IMediator mediator,
            ILogger<WriteController> logger,
            IBaseRepository<CangKuinfo> cangRepsoitory,
            IBaseRepository<CangTypeinfo> cangTypeRepository) // 注入仓库类型仓储
        {
            Mediator = mediator;
            Logger = logger;
            this.cangRepsoitory = cangRepsoitory;
            _cangTypeRepository = cangTypeRepository;
        }

        public IMediator Mediator { get; }
        public ILogger<WriteController> Logger { get; }
        [HttpPost]
        [AllowAnonymous]
        public async Task<APIRequst<int>> Handle(CreateTimeCommand request )
        {
           return await Mediator.Send(request);
        }

        [HttpPost]
        [AllowAnonymous]
        public IActionResult FileWiter(IFormFile file)
        {
            var n = Guid.NewGuid();
            var files = Directory.GetCurrentDirectory() + "/wwwroot/Img" + n + file.FileName;
            using (var stream = new FileStream(files, FileMode.Create))
            {
                file.CopyTo(stream);
                stream.Flush();
            }
            return Ok("http://localhost:5217" + "/Img" + n + file.FileName);
        }
        [HttpPost]
        public async Task<APIRequst<int>> AccountCreate(AccountCreateCommand request)
        {

            return await Mediator.Send(request);
        }
        /// <summary>
        /// 处理删除命令
        /// </summary>
        /// <param name="request">命令</param>
        /// <param name="cancellationToken">取消</param>
        /// <returns></returns>
        [HttpDelete]
        public async Task<APIRequst<int>> AccountDel([FromQuery] AccountDelCommand request)
        {
            return await Mediator.Send(request);
        }

        /// <summary>
        /// 处理账户更新命令
        /// </summary>
        /// <param name="request"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        [HttpPut]
        public async Task<APIRequst<int>> AccountUpdate(AccountUpdateCommand request)
        {
            request.Data.UpdateName = User.FindFirstValue("UserNickname");
            return await Mediator.Send(request);
        }
        /// <summary>
        /// 新增角色
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<APIRequst<int>> RoleCreate(RoleCreateCommand request)
        {
            request.Data.CreateName = User.FindFirstValue("UserNickname");
            return await Mediator.Send(request);
        }
        [HttpDelete]
        /// <summary>
        /// 删除角色
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        public async Task<APIRequst<int>> RoleDel(RoleDelCommand request)
        {
            request.Name = User.FindFirstValue("UserNickname");
            return await Mediator.Send(request);


        }
        /// <summary>
        /// 角色修改
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        [HttpPut]
        public Task<APIRequst<int>> RoleUpdate(RoleUpdateCommand request)
        {
            request.Data.UpdateName = User.FindFirstValue("UserNickname");
            return Mediator.Send(request);
        }
        /// <summary>
        /// 新增组织
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<APIRequst<int>> OrcanCreate(OrcanCreateCommand request)
        {
            request.Data.CreateName = User.FindFirstValue("UserNickname");
            return await Mediator.Send(request);
        }

        /// <summary>
        /// 删除组织
        /// </summary>
        /// <param name="request"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        [HttpDelete]
        public async Task<APIRequst<int>> OrcanDel(OrcanDelCommand request)
        {
            request.Name = User.FindFirstValue("UserNickname");
            return await Mediator.Send(request);

        }
        /// <summary>
        /// 修改组织
        /// </summary>
        /// <param name="request"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        [HttpPut]
        public async Task<APIRequst<int>> OrcanUpdate(OrcanUpdateCommand request)
        {
            request.Data.UpdateName = User.FindFirstValue("UserNickname");
            return await Mediator.Send(request);
        }
        [HttpDelete]
        public async Task<APIRequst<int>> FactoryDel(FactoryDelCommand request)
        {
            request.Name = User.FindFirstValue("UserNickname");
            return await Mediator.Send(request);
        }
        [HttpPut]
        public async Task<APIRequst<int>> FactoryUpdate(FactoryUpdateCommand request)
        {
            request.Data.UpdateName = User.FindFirstValue("UserNickname");
            return await Mediator.Send(request);
        }

        [HttpPost]
        public Task<APIRequst<int>> AddCheJian(AddCheJianCommand command)
        {

            command.CreateName = User.FindFirstValue("UserNickname");
            return Mediator.Send(command);
        }
        [HttpPost]
        public Task<APIRequst<int>> DeleCheJian(DeleCheJianCommand command)
        {
            return Mediator.Send(command);
        }
        [HttpPost]
        public Task<APIRequst<int>> UpdateChejian(UpdateCheJianCommand command)
        {
            command.CreateName = User.FindFirstValue("UserNickname");
            return Mediator.Send(command);
        }
        #region 质量管理
        /// <summary>
        /// 新增检验类型
        /// </summary>
        /// <param name="command">命令</param>
        /// <returns>返回相应的任务</returns>
        [HttpPost]
        public async Task<APIRequst<int>> AddExamineType(CreateExamineTypeCommand command)
        {
            command.CreateName = User.FindFirstValue("UserNickname");
            return await Mediator.Send(command);
        }
        /// <summary>
        /// 逻辑删除检验类型
        /// </summary>
        /// <param name="command">命令</param>
        /// <returns>返回相应的任务</returns>
        [HttpDelete]
        public Task<APIRequst<int>> DeleteType([FromQuery] DeleteExamineTypeCommand command)
        {
            command.DeleteName = User.FindFirstValue("UserNickname");
            return Mediator.Send(command);
        }
        /// <summary>
        /// 修改检验类型
        /// </summary>
        /// <param name="command">命令</param>
        /// <returns>返回相应的任务</returns>
        [HttpPost]
        public Task<APIRequst<int>> UpdateType(UpdateExamineTypeCommand command)
        {
            command.CreateName = User.FindFirstValue("UserNickname");
            return Mediator.Send(command);
        }
        /// <summary>
        /// 检验类型逻辑批量删除
        /// </summary>
        /// <param name="textArray">编号数组</param>
        /// <returns>返回任务</returns>
        [HttpDelete]
        public Task<APIRequst<int>> AllDelete(string textArray)
        {
            AllDeleteExamineTypeCommand command = new AllDeleteExamineTypeCommand();
            command.textArray = textArray;

            return Mediator.Send(command);
        }
        /// <summary>
        /// 新增检验项目信息
        /// </summary>
        /// <param name="command">命令</param>
        /// <returns>返回对应任务</returns>
        [HttpPost]
        public async Task<APIRequst<int>> AddProject(CreateExamineProjectCommand command)
        {
            command.CreateName = User.FindFirstValue("UserNickname");
            return await Mediator.Send(command);
        }
        /// <summary>
        /// 逻辑删除检验项目
        /// </summary>
        /// <param name="command">命令</param>
        /// <returns>返回相应的任务</returns>
        [HttpDelete]
        public Task<APIRequst<int>> DeleteProject([FromQuery] DeleteExamineProjectCommand command)
        {
            command.DeleteName = User.FindFirstValue("UserNickname");
            return Mediator.Send(command);
        }
        /// <summary>
        /// 修改检验项目
        /// </summary>
        /// <param name="command">命令</param>
        /// <returns>返回相应的任务</returns>
        [HttpPost]
        public Task<APIRequst<int>> UpdateProject(UpdateExamineProjectCommand command)
        {
            command.UpdateName = User.FindFirstValue("UserNickname");
            return Mediator.Send(command);
        }
        /// <summary>
        /// 检验项目逻辑批量删除
        /// </summary>
        /// <param name="projectarray">编号数组</param>
        /// <returns>返回任务</returns>
        [HttpDelete]
        public Task<APIRequst<int>> AllDeleteProject(string projectarray)
        {
            AllowExamineProjectCommand command = new AllowExamineProjectCommand();
            command.projectarray = projectarray;
            return Mediator.Send(command);
        }
        /// <summary>
        /// 新增检验方案信息
        /// </summary>
        /// <param name="command">命令</param>
        /// <returns>返回任务</returns>
        [HttpPost]
        public Task<APIRequst<int>> AddExamineDesign(CreateExamineDesignCommand command)
        {
            return Mediator.Send(command);
        }
        /// <summary>
        /// 逻辑删除检验方案
        /// </summary>
        /// <param name="designId">主键</param>
        /// <returns>返回任务</returns>
        [HttpDelete]
        public Task<APIRequst<int>> DeleteExaminDesign(int designId)
        {
            DeleteExamineDesignCommand command = new DeleteExamineDesignCommand();
            command.designId = designId;
            return Mediator.Send(command);
        }
        /// <summary>
        /// 修改检验方案
        /// </summary>
        /// <param name="command">命令</param>
        /// <returns>返回任务</returns>
        [HttpPost]
        public Task<APIRequst<int>> UpdateExamineDesign(UpdateExamineDesignCommand command)
        {
            return Mediator.Send(command);

        }
        /// <summary>
        /// 检验方案批量删除操作
        /// </summary>
        /// <param name="designs">数组</param>
        /// <returns>返回任务</returns>
        [HttpDelete]
        public Task<APIRequst<int>> AllDeleteExamineDesign(string designs)
        {
            AllDeleteExamineDesignCommand command = new AllDeleteExamineDesignCommand();
            command.designs = designs;
            return Mediator.Send(command);
        }
        /// <summary>
        /// 新增检验任务
        /// </summary>
        /// <param name="command">命令</param>
        /// <returns>返回任务</returns>
        [HttpPost]
        public async Task<APIRequst<int>> AddExamineTask(CreateExaminetaskCommand command)
        {
            return await Mediator.Send(command);
        }
        /// <summary>
        /// 逻辑删除检验任务
        /// </summary>
        /// <param name="designId">主键</param>
        /// <returns>返回任务</returns>
        [HttpDelete]
        public Task<APIRequst<int>> DeleteExaminTask(int taskId)
        {

            DeleteExamineTaskCommand command = new DeleteExamineTaskCommand();
            command.taskId = taskId;
            command.DeleteName = User.FindFirstValue("UserNickname");
            return Mediator.Send(command);
        }
        /// <summary>
        /// 修改检验任务
        /// </summary>
        /// <param name="command">命令</param>
        /// <returns>返回任务</returns>
        [HttpPost]
        public Task<APIRequst<int>> UpdateExamineTask(UpdateExamineTaskCommand command)
        {
            return Mediator.Send(command);

        }
        /// <summary>
        /// 检验方案批量删除操作
        /// </summary>
        /// <param name="designs">数组</param>
        /// <returns>返回任务</returns>
        [HttpDelete]
        public Task<APIRequst<int>> AllDeleteExamineTask(string taskcode)
        {
            AllDeleteTaskCommand command = new AllDeleteTaskCommand();
            command.taskcode = taskcode;
            return Mediator.Send(command);
        }
        /// <summary>
        /// 新增来料检验
        /// </summary>
        /// <param name="command">命令</param>
        /// <returns>返回任务</returns>
        [HttpPost]
        public async Task<APIRequst<int>> AddMaterival(ExamineMaterisalCommand command)
        {
            command.CreateName = User.FindFirstValue("UserNickname");
            return await Mediator.Send(command);
        }
        /// <summary>
        /// 逻辑删除来料检验
        /// </summary>
        /// <param name="materialsId">编号</param>
        /// <returns>返回任务</returns>
        [HttpDelete]
        public Task<APIRequst<int>> DeleteMaterival(int materialsId)
        {
            DeleteMaterialsExamineCommand command = new DeleteMaterialsExamineCommand();
            command.materialsId = materialsId;
            command.DeleteName = User.FindFirstValue("UserNickname");
            return Mediator.Send(command);

        }

        /// <summary>
        /// 来料检验批量删除操作
        /// </summary>
        /// <param name="codement">数组</param>
        /// <returns>返回任务</returns>
        [HttpDelete]
        public Task<APIRequst<int>> AllDeleteExamineMater(string codement)
        {
            AllDeleteMaterialCommand command = new AllDeleteMaterialCommand();
            command.codement = codement;
            return Mediator.Send(command);
        }

        #endregion
        [HttpPost]
        [AllowAnonymous]
        public Task<APIRequst<int>> PLDeleCheJian(PLDeleCheJianCommand command)
        {
            return Mediator.Send(command);
        }
        [AllowAnonymous]
        [HttpPost]
        public async Task<APIRequst<UserDTO>> Login(LoginCommand request)
        {
            // 获取客户端IP地址
            // 首先尝试从HTTP头中获取代理传递的真实IP
            var ipAddress = Request.HttpContext.Connection.RemoteIpAddress?.ToString();
            
            // 如果有代理，尝试从常见的代理头中获取真实IP
            if (Request.Headers.ContainsKey("X-Forwarded-For"))
            {
                var forwardedIps = Request.Headers["X-Forwarded-For"].ToString().Split(',');
                if (forwardedIps.Length > 0)
                {
                    ipAddress = forwardedIps[0].Trim();
                }
            }
            
            // 设置客户端IP地址
            request.ClientIp = ipAddress;
            
            return await Mediator.Send(request);
        }
        /// <summary>
        /// 添加BOM
        /// </summary>
        /// <param name="command">添加BOM命令</param>
        /// <returns>操作结果</returns>
        [HttpPost]
        public async Task<APIRequst<int>> AddBOM(AddBOMCommand command)
        {
            return await Mediator.Send(command);
        }

        /// <summary>
        /// 更新BOM
        /// </summary>
        /// <param name="command">更新BOM命令</param>
        /// <returns>操作结果</returns>
        [HttpPost]
        public async Task<APIRequst<int>> UpdateBOM(UpdateBOMCommand command)
        {
            return await Mediator.Send(command);
        }

        [HttpDelete]
        public async Task<APIRequst<int>> DeleteBOM(int bomId)
        {
            DeleteBOMCommand command = new DeleteBOMCommand();
            command.DeleteName = User.FindFirstValue("UserNickname");
            command.BomId = bomId;
            return await Mediator.Send(command);
        }

        #region 客户管理
        /// <summary>
        /// 添加客户信息
        /// </summary>
        /// <param name="command">客户信息命令</param>
        /// <returns>操作结果</returns>
        [HttpPost]
        public async Task<APIRequst<int>> AddCustomer(AddCustomerCommand command)
        {
            // 设置创建人信息
            command.CreateName = User.FindFirstValue("UserNickname");
            return await Mediator.Send(command);
        }

        /// <summary>
        /// 删除客户信息
        /// </summary>
        /// <param name="command">删除命令，包含客户ID</param>
        /// <returns>操作结果</returns>
        [HttpPost]
        public Task<APIRequst<int>> DeleCustomer(DeleCustomerCommand command)
        {
            return Mediator.Send(command);
        }

        /// <summary>
        /// 批量删除客户
        /// </summary>
        /// <param name="command"></param>
        /// <returns></returns>
        [HttpPost]
        public Task<APIRequst<int>> PLDeleCustomer(PLDeleCustomerCommand command)
        {
            return Mediator.Send(command);
        }
        /// <summary>
        /// 更新客户信息
        /// </summary>
        /// <param name="command">更新命令，包含客户所有更新信息</param>
        /// <returns>操作结果</returns>
        [HttpPost]
        public Task<APIRequst<int>> UpdateCustomer(UpdateCustomerCommand command)
        {
            command.CreateName = User.FindFirstValue("UserNickname");
            return Mediator.Send(command);
        }
        #endregion

        #region 产线管理
        /// <summary>
        /// 添加产线信息
        /// </summary>
        /// <param name="command">产线信息命令</param>
        /// <returns>操作结果</returns>
        [HttpPost]
        public async Task<APIRequst<int>> AddChanXian(AddChanXianCommand command)
        {
            // 设置创建人信息
            command.CreateName = User.FindFirstValue("UserNickname");
            return await Mediator.Send(command);
        }

        /// <summary>
        /// 删除产线信息
        /// </summary>
        /// <param name="command">删除命令，包含产线ID</param>
        /// <returns>操作结果</returns>
        [HttpPost]
        public Task<APIRequst<int>> DeleChanXian(DeleChanXianCommand command)
        {
            // 设置删除人信息
            return Mediator.Send(command);
        }

        /// <summary>
        /// 更新产线信息
        /// </summary>
        /// <param name="command">更新命令，包含产线所有更新信息</param>
        /// <returns>操作结果</returns>
        [HttpPost]
        public Task<APIRequst<int>> UpdateChanXian(UpdateChanXianCommand command)
        {
            return Mediator.Send(command);
        }
        #endregion
        #region 物料群组管理
        /// <summary>
        /// 添加物料群组
        /// </summary>
        /// <param name="command">物料群组信息命令</param>
        /// <returns>操作结果</returns>
        [HttpPost]
        public async Task<APIRequst<int>> AddMaterialGroup(AddMaterialGroupCommand command)
        {
            // 设置创建人信息
            command.CreateName = User.FindFirstValue("UserNickname");
            return await Mediator.Send(command);
        }

        /// <summary>
        /// 删除物料群组
        /// </summary>
        /// <param name="command">删除命令，包含物料群组ID</param>
        /// <returns>操作结果</returns>
        [HttpPost]
        public Task<APIRequst<int>> DeleteMaterialGroup(DeleteMaterialGroupCommand command)
        {
            return Mediator.Send(command);
        }

        /// <summary>
        /// 更新物料群组
        /// </summary>
        /// <param name="command">更新命令，包含物料群组所有更新信息</param>
        /// <returns>操作结果</returns>
        [HttpPost]
        public Task<APIRequst<int>> UpdateMaterialGroup(UpdateMaterialGroupCommand command)
        {
            command.CreateName = User.FindFirstValue("UserNickname");
            return Mediator.Send(command);
        }
        #endregion
        /// <summary>
        /// 添加产品物料
        /// </summary>
        /// <param name="command">添加命令</param>
        /// <returns>操作结果</returns>
        [HttpPost]
        public Task<APIRequst<int>> AddProductMaterial(AddProductMaterialCommand command)
        {
            command.CreateName = User.FindFirstValue("UserNickname");
            return Mediator.Send(command);
        }

        /// <summary>
        /// 更新产品物料
        /// </summary>
        /// <param name="command">更新命令</param>
        /// <returns>操作结果</returns>
        [HttpPost]
        public Task<APIRequst<int>> UpdateProductMaterial(UpdateProductMaterialCommand command)
        {
            command.CreateName = User.FindFirstValue("UserNickname");
            return Mediator.Send(command);
        }

        /// <summary>
        /// 删除产品物料（软删除）
        /// </summary>
        /// <param name="command">删除命令</param>
        /// <returns>操作结果</returns>
        [HttpPost]
        public Task<APIRequst<int>> DeleteProductMaterial(DeleteProductMaterialCommand command)
        {
            return Mediator.Send(command);
        }
        [HttpPost]
        public async Task<APIRequst<int>> AddKuqu(AddCangKuQUCommand command)
        {
            command.CreateName = User.FindFirstValue("UserNickname");
            return await Mediator.Send(command);
        }
        [HttpPost]
        public async Task<APIRequst<int>> UpdateKuqu( UpdateCangKuQUCommand command)
        {
            command.CreateName = User.FindFirstValue("UserNickname");
            return await Mediator.Send(command);
        }
        [HttpPost]
        public async Task<APIRequst<int>> DeleteKuqu(DeleteCangKuQUCommand delete)
        {
            return await Mediator.Send(delete);
        }

        /// <summary>
        /// 批量删除产线
        /// </summary>
        /// <param name="command">批量删除产线命令</param>
        /// <returns>操作结果</returns>
        [HttpPost]
        public Task<APIRequst<int>> PLDeleChanXian(PLDeleChanXianCommand command)
        {
            return Mediator.Send(command);
        }

        /// <summary>
        /// 批量删除产品物料
        /// </summary>
        /// <param name="command">批量删除产品物料命令</param>
        /// <returns>操作结果</returns>
        [HttpPost]
        public Task<APIRequst<int>> PLDeleteProductMaterial(PLDeleteProductMaterialCommand command)
        {
            return Mediator.Send(command);
        }

        /// <summary>
        /// 批量删除仓库
        /// </summary>
        /// <param name="command"></param>
        /// <returns></returns>
        [HttpPost]
        public Task<APIRequst<int>> PLDeleCangKu(PLDeleCangKuCommand command)
        {
            return Mediator.Send(command);
        }
        [AllowAnonymous]
        [HttpPost]
        public IActionResult Upload(IFormFile file)
        {
            var stream =new MemoryStream();
            stream.Position = 0;
            file.CopyTo(stream);
            var list = stream.Query<CangKuinfo>().ToList();
            var su = list.Select(x=>new CangKuinfo 
            {
                CangkuNo = x.CangkuNo,
                CangName = x.CangName,
                CangTypeId = x.CangTypeId,
                Address = x.Address,
                MShu = x.MShu,
                Role = x.Role
            }).ToList();
            var sa= cangRepsoitory.AddRange(su);
            return Ok(su);

        }
        [HttpPost]
        public Task<APIRequst<int>> UpdateCangku(UpdateCangKuCommand update)
        {
            update.CreateName = User.FindFirstValue("UserNickname");
            return Mediator.Send(update);
        }
        [HttpPost]
        public Task<APIRequst<int>> AddCangku(AddCangKuCommand command)
        {
            command.CreateName= User.FindFirstValue("UserNickname");
            return Mediator.Send(command);
        }
        [HttpPost]
        public Task<APIRequst<int>> DeleteCangku(DeleteCangKuCommand dele)
        {
            return Mediator.Send(dele);
        }
        [HttpPost]
        public Task<APIRequst<int>> PDeleKuqu(PDeleKuQuCommand command)
        {
            return Mediator.Send(command);
        }
        [HttpPost]
        public Task<APIRequst<int>> AddChuwei(AddChuWeiCommand add)
        {
            add.CreateName= User.FindFirstValue("UserNickname");
            return Mediator.Send(add);
        }
        [HttpPost]
        public Task<APIRequst<int>> DeleChuwei(DeleteChuWeiCommand Dele)
        {
            return Mediator.Send(Dele);
        }
        [HttpPost]
        public Task<APIRequst<int>> UpdateChuwei(UpdateChuWeiCommand update)
        {
            update.CreateName = User.FindFirstValue("UserNickname");
            return Mediator.Send(update);
        }
        [HttpPost]
        public Task<APIRequst<int>> plDeleChuwei(PlChuWeiCommand dele)
        {
            return Mediator.Send(dele);
        }
        [HttpPost]
        public Task<APIRequst<int>> Json(NodeCommand node)
        {
            return Mediator.Send(node);
        }
        /// <summary>
        ///审批
        /// </summary>
        /// <param name="command"></param>
        /// <returns></returns>
        [HttpPost]
        public Task<APIRequst<int>> AddShenPi(AddShenPiCommand command)
        {
            command.PersonId = int.Parse(User.FindFirstValue("UserId"));
            command.PersonName= User.FindFirstValue("UserNickname");
            return Mediator.Send(command);
        }
        [HttpPost]
        public Task<APIRequst<int>> UpdateShenPi(UpdateShenpiCommand command)
        {
          
            command.PersonId = int.Parse(User.FindFirstValue("UserId"));
            command.PersonName = User.FindFirstValue("UserNickname");
            return Mediator.Send(command);
        }
        /// <summary>
        /// 新增下级编号
        /// </summary>
        /// <param name="codeCommand">命令</param>
        /// <returns>返回任务</returns>
        [HttpPost]
        public Task<APIRequst<int>> AddBomLower(AddCodeCommand codeCommand)
        {
            return Mediator.Send(codeCommand);
        }
        [AllowAnonymous]
        [HttpPost]
        public Task<APIRequst<int>> AddRuku(RukuCommand command)
        {
            return Mediator.Send(command);
        }
        [HttpPost]
        public Task<APIRequst<int>> AddFenpei(FenPeiCommand command)
        {
            return Mediator.Send(command);
        }
        [AllowAnonymous]
        [HttpPost]
        public Task<APIRequst<int>> AddShangp(AddGoodCommand command)
        {
            return Mediator.Send(command);
        }
        [HttpPost]
        public async Task<APIRequst<int>> AddFenpeiCangku(FenPeiCommand command)
        {
            return await Mediator.Send(command);
        }
        /// <summary>
        /// 添加Tel表数据
        /// </summary>
        /// <param name="command">添加Tel表数据命令</param>
        /// <returns>操作结果</returns>
        [HttpPost]
        [AllowAnonymous]
        public async Task<APIRequst<int>> AddTel(AddTelCommand command)
        {
            return await Mediator.Send(command);
        }
        /// <summary>
/// 添加供应商信息
/// </summary>
/// <param name="command">供应商信息命令</param>
/// <returns>操作结果</returns>
[HttpPost]
public async Task<APIRequst<int>> AddSupplier(AddSupplierCommand command)
{
    // 设置创建人信息
    command.CreateName = User.FindFirstValue("UserNickname");
    return await Mediator.Send(command);
}

/// <summary>
/// 删除供应商信息
/// </summary>
/// <param name="command">删除命令，包含供应商ID</param>
/// <returns>操作结果</returns>
[HttpPost]
public Task<APIRequst<int>> DeleSupplier(DeleSupplierCommand command)
{
    return Mediator.Send(command);
}

/// <summary>
/// 批量删除供应商信息
/// </summary>
/// <param name="command">批量删除命令，包含供应商ID列表</param>
/// <returns>操作结果</returns>
[HttpPost]
public Task<APIRequst<int>> PLDeleSupplier(PLDeleSupplierCommand command)
{
    return Mediator.Send(command);
}

/// <summary>
/// 更新供应商信息
/// </summary>
/// <param name="command">更新命令，包含供应商所有更新信息</param>
/// <returns>操作结果</returns>
[HttpPost]
public Task<APIRequst<int>> UpdateSupplier(UpdateSupplierCommand command)
{
    command.CreateName = User.FindFirstValue("UserNickname");
    return Mediator.Send(command);
}
        [HttpPost] 
        public Task<APIRequst<int>> Chuku(ChuKuCommand command)
        {
            return  Mediator.Send(command);
        }

        /// <summary>
        /// 更新成品出库信息
        /// </summary>
        /// <param name="command">更新命令</param>
        /// <returns>操作结果</returns>
        [HttpDelete]
        public Task<APIRequst<int>> UpdateChuku(string sid,int productCount)
        {
            UpdateChukuCommand command = new UpdateChukuCommand();
            command.SID=sid;
            command.ProductCount=productCount;
            return Mediator.Send(command);
        }


    }

}
// 在文件顶部添加引用

// 在控制器类中添加以下方法


                
            
                
            
    

   






